﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

/************��ҵ��������****************/

// ����ѧ�� 
void addStudent();
// ɾ��ѧ������ɾ���� 
void deleteStudent();
// ��ѯ 
void queryStudent();
// �޸���Ϣ 
void updateStudent();
// ��ʾ������Ϣ
void printAll();
// ���ļ� 
int readData();
// д�ļ� 
int writeData();
// �ɼ�����
void analysis();

/******************����ҵ��������***************/
// ����ѧ�� 
int createSno();
// ������Ų������� 
int findIndex(int sno);
// ��ӡ��ǰ��������Ϣ 
void printCurrent(int index);
// ��ӭ
void welcome();
// ���˵�
void mainmenu();

/*********������֤*********/

// ��ȡ����
void getName(char name[]);
// ��ȡ�Ա�
void getSex(char sex[]);
// ��ȡ�༶
void getClass1(char class[]);
// ��ȡ�ɼ�
int getScore(int course);
// ��ȡѡ�� 
int getSelect();

/***********���ݽṹ��ȫ�ֱ���************/
struct student
{
    int sno;
    char name[20];
    char sex[3];
    char class[10];
    int chinese;
    int maths;
    int english;
    int total;
    int state;//״̬��1������0ɾ����  
};
//����ȫ�ֱ������洢100��ѧ����Ϣ
struct student  students[100];
//����±�
int maxIndex = -1;
//�ļ�ָ��
FILE* fp;

main()
{

    welcome();//��ӭ����
    maxIndex = readData();//��ҵ��ѡ�����ǰ���ļ�����������±�
    mainmenu();
}
/*****************************
�������ܣ���ӭ���棬����������ʾ
��������
����ֵ����
*****************************/
void welcome()
{
    printf("|-------------------------------|\n");
    printf("|    ��ӭ����ѧ������ϵͳ       |\n");
    printf("|-------------------------------|\n");
}
/**************************************
�������ܣ���ʾҵ��˵�
��������
����ֵ����
***************************************/
void mainmenu()
{
    int select = 0;;
    while (1)
    {
        printf("|--------------------------|\n");
        printf("|    1 ����ѧ����Ϣ        |\n");
        printf("|    2 ɾ��ѧ����Ϣ        |\n");
        printf("|    3 �޸�ѧ����Ϣ        |\n");
        printf("|    4 ����ѧ����Ϣ        |\n");
        printf("|    5 ��ӡȫ����Ϣ        |\n");
        printf("|    6 ͳ�Ƴɼ���Ϣ        |\n");
        printf("|    0 �˳�ϵͳ            |\n");
        printf("|--------------------------|\n");
        fflush(stdin);//��ջ���������ֹ�����ַ���
        //scanf("%d",&select);
        select = getSelect();
        switch (select)
        {
        case 1:
            addStudent();
            break;
        case 2:
            deleteStudent();
            break;
        case 3:
            updateStudent();
            break;
        case 4:
            queryStudent();
            break;
        case 5:
            printAll();
            break;
        case 6:
            analysis();
            break;
        case 0:
            exit(0);
        default:
            printf("����ѡ������\n");
        }
    }
}
/***********************************
�������ܣ�����ѧ���ɼ���Ϣ
��������
����ֵ����
************************************/

void  addStudent()
{
    printf("----------      ����ѧ��       -----------\n");
    char answer;
    int flag = 0;//д�ļ��ɹ���־
    while (1)
    {
        maxIndex++;//��������֮ǰ�±�����1
        //ѧ���Զ����� 
        students[maxIndex].sno = createSno();

        getName(students[maxIndex].name);

        getSex(students[maxIndex].sex);

        getClass1(students[maxIndex].class);

        students[maxIndex].chinese = getScore(1);

        students[maxIndex].maths = getScore(2);

        students[maxIndex].english = getScore(3);
        //�����ܳɼ�
        students[maxIndex].total = students[maxIndex].chinese +
            students[maxIndex].maths +
            students[maxIndex].english;
        //����״̬Ϊ1
        students[maxIndex].state = 1;
        //��ʾ��ǰѧ����Ϣ
        printCurrent(maxIndex);
        //д�ļ�
        flag = writeData();
        if (flag == 1)
        {
            printf("���ӳɹ���\n");
        }
        else
        {
            printf("����ʧ�ܣ�\n");
        }
        //��ʾ�Ƿ����
        printf("�Ƿ��������(y)��(n):");
        fflush(stdin);
        scanf("%c", &answer);
        if (answer == 'y')
        {
            continue;
        }
        else
        {
            break;
        }

    }
}
/***********************************
�������ܣ�ɾ��ѧ���ɼ���Ϣ
��������
����ֵ����
************************************/
void deleteStudent()
{
    int sno;
    int index;
    int answer;
    printf("----------      ɾ��ѧ��       -----------\n");
    printf("������Ҫɾ��ѧ����ѧ�ţ�");
    scanf("%d", &sno);
    index = findIndex(sno);
    if (index == -1)
    {
        printf("û�и�ѧ����Ϣ��\n");
        return;
    }
    //��ӡ��Ϣ
    printCurrent(index);
    printf("��ȷ��Ҫɾ����1�� 0��");
    scanf("%d", &answer);
    if (answer == 1) {
        //ִ��ɾ��
        students[index].state = 0;
        //д�ļ�
        writeData();
        printf("ɾ���ɹ���\n");
    }
}
/***********************************
�������ܣ����ݹؼ��ֲ���ѧ���ɼ���Ϣ
��������
����ֵ����
************************************/
void queryByName()
{
    char name[20];
    int i;
    int flag = 0;

    printf("���������ѧ����������");
    fflush(stdin);
    gets(name);
    for (i = 0; i <= maxIndex; i++) {
        if (strstr(students[i].name, name) != NULL) {
            printCurrent(i);
            flag = 1;
        }
    }
    if (flag == 0) {
        printf("û�в��ҵ���Ϣ��\n");
    }
}

void queryById()
{
    int sno = 0;
    int i = 0;
    int flag = 0;
    printf("������Ҫ����ѧ����ѧ�ţ�");
    scanf("%d", &sno);
    for (i = 0; i <= maxIndex; i++) {
        if (sno == students[i].sno) {
            printCurrent(i);
            flag = 1;
        }
    }
    if (flag == 0) {
        printf("û�в��ҵ���Ϣ��\n");
    }
}
void queryStudent()
{
    int select = 0;
    printf("----------      ����ѧ��       -----------\n");
    printf("--- 1 ��ѧ��    2 ������ ---\n");
    scanf("%d", &select);
    if (select == 1) {
        queryById();
    }
    else {
        queryByName();
    }

}

/***********************************
�������ܣ��޸�ѧ���ɼ���Ϣ
��������
����ֵ����
************************************/
void updateStudent()
{
    int sno;

    printf("----------      �޸�ѧ��       -----------\n");
    printf("������Ҫ�޸�ѧ����ѧ�ţ�");
    scanf("%d", &sno);
    int index = findIndex(sno);
    if (index == -1)
    {
        printf("û�и�ѧ����Ϣ��\n");
        return;
    }
    //��ӡ��Ϣ
    printCurrent(index);
    printf("����������ѧ����Ϣ��\n");
    getName(students[index].name);

    getSex(students[index].sex);

    getClass1(students[index].class);

    students[index].chinese = getScore(1);

    students[index].maths = getScore(2);

    students[index].english = getScore(3);
    //�����ܳɼ�
    students[index].total = students[index].chinese +
        students[index].maths +
        students[index].english;
    //��ʾ��ǰѧ����Ϣ
    printCurrent(index);
    //д�ļ�
    int flag = writeData();
    if (flag == 1)
    {
        printf("�޸ĳɹ���\n");
    }
    else
    {
        printf("�޸�ʧ�ܣ�\n");
    }
}

/*****************************************
�������ܣ����ļ������ļ��е���Ϣ����������
��������
����ֵ�����ض������������±� ,����ǵ�һ��
         ���У���Ҫ�½��ļ�������-1
*****************************************/
int readData()
{
    int i = 0;
    //���ļ�
    if ((fp = fopen("studentinfo.txt", "r")) == NULL)
    {
        //��д�ķ�ʽ���ļ�
        fopen("studentinfo.txt", "w");
        return -1;//��������±�ĳ�ֵ 
    }
    //���ļ�,��ȷ���ļ��ж�����Ϣ��ʹ��ѭ��
    while (1)
    {
        //�ж��ļ�����
        if (feof(fp))
        {
            break;
        }
        fscanf(fp, "%d %s %s %s %d %d %d %d %d",
            &students[i].sno,
            students[i].name,
            students[i].sex,
            students[i].class,
            &students[i].chinese,
            &students[i].maths,
            &students[i].english,
            &students[i].total,
            &students[i].state);
        i++;//����һ�����ݺ��±���1
    }
    //�ر��ļ�
    fclose(fp);
    return i - 2;//���1��,�±��ֱ�ʵ��������1 
}
/***********************************
�������ܣ�д�ļ�
��������
����ֵ��д�ļ��ɹ�����1��ʧ�ܷ���0
************************************/
int writeData()
{
    int i;
    //���ļ�
    if ((fp = fopen("studentinfo.txt", "w")) == NULL)
    {
        //��ʾ����
        printf("���ļ�ʧ�ܣ�");
        return 0;
    }
    //д�ļ�
    for (i = 0; i <= maxIndex; i++)
    {
        fprintf(fp, "%d %s %s %s %d %d %d %d %d\n",
            students[i].sno,
            students[i].name,
            students[i].sex,
            students[i].class,
            students[i].chinese,
            students[i].maths,
            students[i].english,
            students[i].total,
            students[i].state);
    }
    //�ر��ļ�
    fclose(fp);
    return 1;
}
/*******************************************
�������ܣ�����ѧ�Ų���ѧ���������е�����λ��
������sno,Ҫ���ҵ�ѧ��
����ֵ��������������ҵ������������Ҳ�������-1
*******************************************/
int findIndex(int sno)
{
    int index = -1;
    int i;
    for (i = 0; i <= maxIndex; i++)
    {
        if (students[i].sno == sno) {
            index = i;
            break;
        }
    }
    return index;
}
/***********************************
�������ܣ���ӡ��ǰѧ���ɼ���Ϣ
������index,��ǰ����ֵ
����ֵ����
************************************/
void printCurrent(int index) //index��ʾ�±�
{
    printf("|-------------------------------------------------------------------------|\n");
    printf("|%-8s|%-10s|%-8s|%-8s|%-8s|%-8s|%-8s|%-8s|\n", "ѧ��", "����", "�Ա�", "�༶", "����", "��ѧ", "Ӣ��", "�ܳɼ�");
    printf("|--------+----------+--------+--------+--------+--------+--------+--------|\n");
    printf("|%-8d|%-10s|%-8s|%-8s|%-8d|%-8d|%-8d|%-8d|\n",
        students[index].sno,
        students[index].name,
        students[index].sex,
        students[index].class,
        students[index].chinese,
        students[index].maths,
        students[index].english,
        students[index].chinese +
        students[index].maths +
        students[index].english);
    printf("|-------------------------------------------------------------------------|\n");
}
/***********************************
�������ܣ���ӡ����ѧ���ɼ���Ϣ
��������
����ֵ����
************************************/
void printAll()
{   //��ӡ״̬Ϊ1��ѧ����Ϣ 
    int i;
    printf("|-------------------------------------------------------------------------|\n");
    printf("|%-8s|%-10s|%-8s|%-8s|%-8s|%-8s|%-8s|%-8s|\n", "ѧ��", "����", "�Ա�", "�༶", "����", "��ѧ", "Ӣ��", "�ܳɼ�");
    printf("|--------+----------+--------+--------+--------+--------+--------+--------|\n");
    for (i = 0; i <= maxIndex; i++)
    {
        if (students[i].state == 1) {
            printf("|%-8d|%-10s|%-8s|%-8s|%-8d|%-8d|%-8d|%-8d|\n",
                students[i].sno,
                students[i].name,
                students[i].sex,
                students[i].class,
                students[i].chinese,
                students[i].maths,
                students[i].english,
                students[i].chinese +
                students[i].maths +
                students[i].english);
            if (i == maxIndex) {
                printf("|-------------------------------------------------------------------------|\n");
            }
            else {
                printf("|--------+----------+--------+--------+--------+--------+--------+--------|\n");
            }
        }


    }
}
/********************************************
�������ܣ��Զ����ɵ���ѧ��ѧ�ţ�����ǵ�һ������
           ���򷵻�10001�����Է�����һ��ѧ����
           ѧ��+1
��������
����ֵ��ѧ��ѧ��
*********************************************/
int createSno()
{
    if (maxIndex == 0) {
        return 10001;
    }
    else {
        return  students[maxIndex - 1].sno + 1;
    }
}

/*************************************



************************************/
void getName(char name[])
{
    char chkName[50];
    while (1) {
        printf("����: ");
        fflush(stdin);
        gets(chkName);
        int length = strlen(chkName);
        if (length > 19) {
            printf("�����������ȣ����ٴ����룺\n");
            continue;
        }
        else {
            strcpy(name, chkName); //��chkName���Ƶ�name
            break;
        }

    }
}
void getSex(char sex[])
{
    char chkSex[50];
    while (1) {
        printf("�Ա�: ");
        fflush(stdin);
        gets(chkSex);
        int length = strlen(chkSex);
        if (length > 2) {
            printf("�Ա𳬳����ȣ����ٴ����룺\n");
            continue;
        }
        else {
            strcpy(sex, chkSex); //��chkSex���Ƶ�sex
            break;
        }
    }
}
void getClass1(char class[])
{
    char chkClass[50];
    while (1) {
        printf("�༶: ");
        fflush(stdin);
        gets(chkClass);
        int length = strlen(chkClass);
        if (length > 9) {
            printf("�༶�������ȣ����ٴ����룺\n");
            continue;
        }
        else {
            strcpy(class, chkClass); //��chkClass���Ƶ�class
            break;
        }
    }
}

int getScore(int course)
{
    char score_s[50];
    int score = 0;
    while (1) {
        if (course == 1) {
            printf("���ĳɼ���");
        }
        else if (course == 2) {
            printf("��ѧ�ɼ���");
        }
        else {
            printf("Ӣ��ɼ���");
        }
        fflush(stdin);
        gets(score_s);
        //������֤
        int length = strlen(score_s);
        int i;
        int flag = 0;
        for (i = 0; i < length; i++) {
            if (!isdigit(score_s[i])) {
                flag = 1;
                break;
            }
        }
        if (flag == 1) {
            printf("�ɼ�����Ƿ������ٴ����룺\n");
            continue;
        }
        score = atoi(score_s);
        if (score < 0 || score>100) {
            printf("�ɼ���Ҫ��0-100֮��");
            continue;
        }

        return score;
    }
}
void getMaxScore()
{
    int maxChinese = students[0].chinese;
    int maxEnglish = students[0].english;
    int maxMaths = students[0].maths;
    int i = 0;
    int chineseIndex = 0;
    int englishIndex = 0;
    int mathsIndex = 0;

    for (i = 1; i <= maxIndex; i++) {
        if (maxChinese < students[i].chinese)
        {
            maxChinese = students[i].chinese;
            chineseIndex = i;
        }
        if (maxEnglish < students[i].english)
        {
            maxEnglish = students[i].english;
            englishIndex = i;
        }
        if (maxEnglish < students[i].maths)
        {
            maxMaths = students[i].maths;
            mathsIndex = i;
        }
    }
    printf("������߳ɼ��� %d\n", maxChinese);
    printCurrent(chineseIndex);
    printf("��ѧ��߳ɼ��� %d\n", maxMaths);
    printCurrent(mathsIndex);
    printf("Ӣ����߳ɼ��� %d\n", maxEnglish);
    printCurrent(englishIndex);
}

void getAvgScore()
{
    double avgChinese = 0;
    double avgMaths = 0;
    double avgEnglish = 0;
    int i = 0;
    for (i = 0; i <= maxIndex; i++)
    {
        avgChinese += students[i].chinese;
        avgMaths += students[i].maths;
        avgEnglish += students[i].english;
    }
    printf("|-----------------------|\n");
    printf("|%-14s| %-7.2lf|\n", "����ƽ���ɼ�:", avgChinese / (maxIndex + 1));
    printf("|--------------+--------|\n");
    printf("|%-14s| %-7.2lf|\n", "��ѧƽ���ɼ�:", avgMaths / (maxIndex + 1));
    printf("|--------------+--------|\n");
    printf("|%-14s| %-7.2lf|\n", "Ӣ��ƽ���ɼ�", avgEnglish / (maxIndex + 1));
    printf("|-----------------------|\n");
}

void analysis()
{
    printf("--------------�ɼ�����-----------\n");
    printf("----������߷�\n");
    getMaxScore();
    printf("----����ƽ����\n");
    getAvgScore();

}
int getSelect()
{
    char select_s[50];
    int select = 0;
    while (1) {
        printf("��ѡ��ҵ��");
        fflush(stdin);
        gets(select_s);
        //������֤
        int length = strlen(select_s);
        if (length > 1) {
            printf("ѡ�޳������ȣ����ٴ����룺\n");
            continue;
        }
        if (!isdigit(select_s[0])) {
            printf("ѡ������Ƿ������ٴ����룺\n");
            continue;
        }
        select = atoi(select_s);
        return select;
    }
}